package com.ruoyi.project.channel.channelSetting.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.project.channel.payerPayType.domain.PayerPayType;
import com.ruoyi.project.channel.payerPayType.service.IPayerPayTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.channelSetting.domain.ChannelSetting;
import com.ruoyi.project.channel.channelSetting.service.IChannelSettingService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 码商配置信息操作处理
 *
 * @author CAIN
 * @date 2019-09-07
 */
@Controller
@RequestMapping("/channel/channelSetting")
public class ChannelSettingController extends BaseController {
    private String prefix = "channel/channelSetting";

    @Autowired
    private IChannelSettingService channelSettingService;
    @Autowired
    private IPayerPayTypeService payerPayTypeService;

    @RequiresPermissions("channel:channelSetting:view")
    @GetMapping("/{channelId}")
    public String channelSetting(@PathVariable("channelId") Long channelId, ModelMap mmap) {
        mmap.put("channelId", channelId);

        return prefix + "/channelSetting";
    }

    /**
     * 查询码商配置列表
     */
    @RequiresPermissions("channel:channelSetting:list")
    @PostMapping("/list/{channelId}")
    @ResponseBody
    public TableDataInfo list(ChannelSetting channelSetting, @PathVariable("channelId") Long channelId) {
        startPage();
        channelSetting.setChannelId(channelId);
        List<ChannelSetting> list = channelSettingService.selectChannelSettingList(channelSetting);
        return getDataTable(list);
    }


    /**
     * 导出码商配置列表
     */
    @RequiresPermissions("channel:channelSetting:export")
    @PostMapping("/export/{channelId}")
    @ResponseBody
    public AjaxResult export(ChannelSetting channelSetting, @PathVariable("channelId") Long channelId) {
        channelSetting.setChannelId(channelId);
        List<ChannelSetting> list = channelSettingService.selectChannelSettingList(channelSetting);
        ExcelUtil<ChannelSetting> util = new ExcelUtil<ChannelSetting>(ChannelSetting.class);
        return util.exportExcel(list, "channelSetting");
    }

    /**
     * 新增码商配置
     */
    @GetMapping("/add/{channelId}")
    public String add(@PathVariable("channelId") Long channelId,ModelMap mmap) {
        mmap.put("channelId", channelId);
        List<PayerPayType> payerPayTypes = payerPayTypeService.selectPayerPayTypeList(new PayerPayType());
        List<PayerPayType> tmp = new ArrayList<>();
        for (int i = 0; i < payerPayTypes.size(); i++) {
            if (payerPayTypes.get(i).getPayerUserName().equals("系统自带")) {
                tmp.add(payerPayTypes.get(i));
            }
        }
        mmap.put("payerPayTypes", tmp);
        return prefix + "/add";
    }

    /**
     * 新增保存码商配置
     */
    @RequiresPermissions("channel:channelSetting:add")
    @Log(title = "码商配置", businessType = BusinessType.INSERT)
    @PostMapping("/add/{channelId}")
    @ResponseBody
    public AjaxResult addSave(ChannelSetting channelSetting,@PathVariable("channelId") Long channelId) {
        channelSetting.setChannelId(channelId);
        return toAjax(channelSettingService.insertChannelSetting(channelSetting));
    }

    /**
     * 修改码商配置
     */
    @GetMapping("/edit/{rateId}")
    public String edit(@PathVariable("rateId") Long rateId, ModelMap mmap) {
        ChannelSetting channelSetting = channelSettingService.selectChannelSettingById(rateId);
        mmap.put("channelSetting", channelSetting);
        List<PayerPayType> payerPayTypes = payerPayTypeService.selectPayerPayTypeList(new PayerPayType());
        List<PayerPayType> tmp = new ArrayList<>();
        for (int i = 0; i < payerPayTypes.size(); i++) {
            if (payerPayTypes.get(i).getPayerUserName().equals("系统自带")) {
                tmp.add(payerPayTypes.get(i));
            }
        }
        mmap.put("payerPayTypes", tmp);
        return prefix + "/edit";
    }

    /**
     * 修改保存码商配置
     */
    @RequiresPermissions("channel:channelSetting:edit")
    @Log(title = "码商配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChannelSetting channelSetting) {
        return toAjax(channelSettingService.updateChannelSetting(channelSetting));
    }

    /**
     * 删除码商配置
     */
    @RequiresPermissions("channel:channelSetting:remove")
    @Log(title = "码商配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(channelSettingService.deleteChannelSettingByIds(ids));
    }

}
